package com.nf.health.app.activity.global;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import com.nf.health.app.R;
import com.nf.health.app.http.Apis;

/**
 * Created by Jason on 2016/5/6.
 */

public class LoadWebActivty2 extends BaseActivity{
    private String  FLAG_REQEST_DATA = "flag_reqest_data";
    private Context context;
    private WebView wb_load_healthy;
    TextView titlebar_title_tv;
    private String url = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContent(R.layout.activity_loadweb_healthy);
        setTitleBar(R.layout.layout_common_header);
        context = LoadWebActivty2.this;
        wb_load_healthy = (WebView)findViewById(R.id.wb_load_healthy);

        wb_load_healthy.setWebChromeClient(new WebChromeClient());

        titlebar_title_tv = (TextView) findViewById(R.id.titlebar_title_tv);
        // ViewUtil.setText(this, R.id.titlebar_title_tv, "恩福设备");
        ProxyBridge pBridge = new ProxyBridge();
        wb_load_healthy.getSettings().setJavaScriptEnabled(true);
        WebSettings webSettings = wb_load_healthy.getSettings();
        //
//        webSettings.setUseWideViewPort(true);
//        webSettings.setLoadWithOverviewMode(true);

        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);
        WebSettings wSet = wb_load_healthy.getSettings();
        wSet.setDomStorageEnabled(true);
        wSet.setDatabaseEnabled(true);
        wSet.setJavaScriptEnabled(true);
        //设置支持缩放
        wSet.setBuiltInZoomControls(true);
        //设置支持加载图片
        wSet.setBlockNetworkImage(false);
        //设置支持缓存
        wSet.setAppCacheEnabled(true);
        //设置缓存模式为默认
        wSet.setCacheMode(WebSettings.LOAD_DEFAULT);

        url = getIntent().getStringExtra("url");
        String title = getIntent().getStringExtra("title");
        titlebar_title_tv.setText(TextUtils.isEmpty(title) ? "恩福" : title);
        if (isNetworkAvailable(context)) {
            wb_load_healthy.setWebViewClient(wvc);
            showLoading();
            wb_load_healthy.loadUrl(url+ Apis.WEBVIEW_PLETEFORM);//"https://www.baidu.com/"
        }else{
            showToast( "请链接网络...");
        }
    }

    public class ProxyBridge {
        public int one () {
            return 1;
        }
    }

    // goBack()表示返回webView的上一页面
   /*public boolean onKeyDown(int keyCoder, KeyEvent event) {
        if (wb_load_healthy.canGoBack() && keyCoder == KeyEvent.KEYCODE_BACK) {
            wb_load_healthy.goBack();
            return true;
        }
        return false;
    }*/
    // 创建WebViewClient对象
    WebViewClient wvc = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // 使用自己的WebView组件来响应Url加载事件，而不是使用默认浏览器器加载页面
            wb_load_healthy.loadUrl(url);
            // 记得消耗掉这个事件。给不知道的朋友再解释一下，Android中返回True的意思就是到此为止吧,事件就会不会冒泡传递了，我们称之为消耗掉
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            //Toast.makeText(getApplicationContext(), "WebViewClient.onPageFinished", Toast.LENGTH_SHORT).show();
            super.onPageFinished(view, url);
            hideLoading();
        }


        @Override
        public void onLoadResource(WebView view, String url) {
            //Toast.makeText(getApplicationContext(), "WebViewClient.onLoadResource", Toast.LENGTH_SHORT).show();
            super.onLoadResource(view, url);
        }
        @Override
        public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
            //Toast.makeText(getApplicationContext(), "WebViewClient.onReceivedError", Toast.LENGTH_SHORT).show();
            super.onReceivedError(view, errorCode, description, failingUrl);
            hideLoading();
        }
        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            //Toast.makeText(getApplicationContext(), "WebViewClient.onScaleChanged", Toast.LENGTH_SHORT).show();
            super.onScaleChanged(view, oldScale, newScale);
        }
    };
    /**
     * 检测当的网络（WLAN、3G/2G）状态
     * @param context Context
     * @return true 表示网络可用
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected())
            {
                // 当前网络是连接的
                if (info.getState() == NetworkInfo.State.CONNECTED)
                {
                    // 当前所连接的网络可用
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        sendBroadcast(new Intent(MainActivity.CLOSE_DIALOG));
    }
}
